package com.biz.service.MService.mebactivity;

import com.biz.model.Hmodel.base.TBaseMebactivityUser;
import com.biz.model.Hmodel.base.TWxGoods;
import com.biz.model.Hmodel.sys.TSysUser;
import com.biz.service.HService.base.BaseServiceImpl;
import com.biz.utils.UuidUtil;
import com.framework.dao.hdao.BaseDaoI;
import com.framework.dao.mdao.DaoSupport;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author lzq
 */
@Service("mebactivityService")
public class MebactivityServiceImpl extends BaseServiceImpl<TWxGoods> implements MebactivityServiceI {
  @Resource(name = "daoSupport")
  private DaoSupport dao;

  @Autowired
  private BaseDaoI<TBaseMebactivityUser> taseMebactivityUserDao;

  @Autowired
  private BaseDaoI<TSysUser> tsysUserDao;

  @Override
  public List<Map<String, Object>> loadMebactivityList() throws Exception {
    String id="";
    return (List<Map<String,Object>>) dao.findForList("MebactivityDao.loadMebactivityList",id);
  }

  @Override
  public Map<String, Object> findDetail(String id) throws Exception {
    return (Map<String, Object>) dao.findForObject("MebactivityDao.loadMebactivityList",id);
  }

  @Override
  public List<Map<String, Object>> getCommentList(String id) throws Exception {
    return (List<Map<String,Object>>) dao.findForList("MebactivityDao.getCommentList",id);
  }

  @Override
  public int getApplyCount(String userId, String id) throws Exception {
    Map<String,Object> map=new HashedMap();
    map.put("userId",userId);
    map.put("id",id);
    return (Integer) dao.findForObject("MebactivityDao.getApplyCount",map);
  }

  @Override
  public String toCommentRecord(String userId, String id, String state) throws Exception {
    Map<String,Object> map=new HashedMap();
    //首先判断活动是否结束
    int activityCount=(Integer) dao.findForObject("MebactivityDao.getActivityCount",id);
    if(activityCount>0) {
      map.put("userId", userId);
      map.put("id", id);
      //判断该用户是否成为该组织的会员
      int count = (Integer) dao.findForObject("MebactivityDao.getMebCount", map);
      if (count == 0) {
        //新增会员
        String groupId = (String) dao.findForObject("MebactivityDao.findGroupId", id);
        map.put("groupId", groupId);
        map.put("mebId", UuidUtil.get32UUID());
        dao.save("MebactivityDao.insertMebList", map);
      }
      if ("1".equals(state)) {
        //取消报名
        dao.update("MebactivityDao.updateCommentState", map);
      } else {
        //报名
        TBaseMebactivityUser t = new TBaseMebactivityUser();
        t.setId(UuidUtil.get32UUID());
        t.setMainId(id);
        t.setUserId(userId);
        taseMebactivityUserDao.save(t);
      }
      return "操作成功";
    }else{
      return "报名失败，活动尚未开始或已经结束！";
    }
  }

  @Override
  public String doSaveComment(String userId, String id, String note) throws Exception {
    Map<String,Object> map=new HashedMap();
    map.put("userId",userId);
    map.put("mainId",id);
    map.put("note",note);
    map.put("id",UuidUtil.get32UUID());
    dao.save("MebactivityDao.doSaveComment",map);
    return "操作成功";
  }
}
